package com.example.logservice.LogController;

import com.alibaba.fastjson.JSON;
import com.example.logservice.LogService.LogService;
import com.example.logservice.LogPojo.Log;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/log")
public class LogController {

    @Resource
    LogService logService;

    //保存日志
    @RequestMapping("/saveLog")
    public int saveLog(String logTarget,String logLevel,String logDetail){
        Log log = new Log(logTarget,logLevel,logDetail);
        return logService.saveLog(log);
    }

    //展示全部日志
    @RequestMapping("/showLog")
    public List<Log> showLog(String logLevel,String selTime) throws ParseException {

//        System.out.println(logService.showLog().toString());

//        String log = JSON.toJSONString(logService.showLog());
        String endTime = "";
        if (selTime != null && selTime.length() != 0){
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            Date time = sf.parse(selTime);
            endTime = sf.format(time.getTime() + (long)24 * 60 * 60 * 1000);
        }
        return logService.showLog(logLevel,selTime,endTime);

//
//        if (logLevel.equals("") && selTime.equals("")){
//            return logService.showLog(logLevel,startTime,endTime);
//        }
//        if (logLevel.equals("") && !selTime.equals("")){
//            String startTime = selTime;
//            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
//            Date time = sf.parse(selTime);
//            String endTime = sf.format(time.getTime() + (long)24 * 60 * 60 * 1000);
//            return logService.selLogByTimes(startTime,endTime);
//        }
//        if (!logLevel.equals("") && selTime.equals("")){
//            return logService.selLogByLevel(logLevel);
//        }
//        if (!logLevel.equals("") && !selTime.equals("")){
//            return logService.selLogByTwice(logLevel,startTime,endTime);
//        }


    }

    //通过状态查询日志
//    @RequestMapping("/selLogByLevel")
//    public List<Log> selLogByLevel(String logLevel){
//
//        return logService.selLogByLevel(logLevel);
//    }
//
//    //通过日期查询日志
//    @RequestMapping("/selLogByTimes")
//    public List<Log> selLogByTimes(String selTime) throws ParseException {
//
//        String startTime = selTime;
//        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
//        Date time = sf.parse(selTime);
//        String endTime = sf.format(time.getTime() + (long)24 * 60 * 60 * 1000);
//
//
//        return logService.selLogByTimes(startTime,endTime);
//    }
//
//    @RequestMapping("/selLogByTwice")
//    public List<Log> selLogByTwice(String logLevel,String selTime) throws ParseException {
//
//        String startTime = selTime;
//        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
//        Date time = sf.parse(selTime);
//        String endTime = sf.format(time.getTime() + (long)24 * 60 * 60 * 1000);
//
//
//        return logService.selLogByTwice(logLevel,startTime,endTime);
//    }

    ////////////////////////////////////////



    //通过状态或日期查询日志
//    @RequestMapping("/showLog")
//    public List<Log> showLog(String key) throws ParseException {
//
//        if (key.equals("info") || key.equals("error")){
//            return logService.selLogByLevel(key);
//        }else {
//            String startTime = key;
//            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
//            Date time = sf.parse(key);
//            String endTime = sf.format(time.getTime() + (long)24 * 60 * 60 * 1000);
//            return logService.selLogByTimes(startTime,endTime);
//        }
//
//    }
//
//
//    @RequestMapping("/showLog")
//    public List<Log> showLog(String logLevel,String selTime) throws ParseException {
//
//        String startTime = selTime;
//        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
//        Date time = sf.parse(selTime);
//        String endTime = sf.format(time.getTime() + (long)24 * 60 * 60 * 1000);
//        return logService.selLogByTwice(logLevel,startTime,endTime);
//    }
}
